var net = require("net.js");
var gd = getApp().globalData;
function get_valid_code(mobile,id,callback){
  if(mobile.length!=11){
    callback.fail(2,"手机号码格式不正确")
    return;
  } if (!id) {
    callback.fail(2, "请选择店铺")
    return;
  }

  net.request({
    url: "/GetCode",
    data:{
       mobile:mobile,
       shop_id:id
    },
    success(res){
      console.log("get_valid_code success",res);
      var dt = res.data;
      if (dt.flag){
        console.log("验证码",dt.data);
        wx.setStorageSync("valid_code", dt.data);
        wx.setStorageSync("valid_code_time", new Date().getTime());
        if (callback.success) callback.success(3, dt.msg);
      }else{
        callback.fail(1, dt.msg);
      }
    },fail(res){
      console.log("get_valid_code fail", res);
      if (callback.fail) callback.fail(1, "网络异常");
    },complete(){
      if(callback.complete) callback.complete();
    }
  })
}

function forgetpsw(mobile,valid, psw1, psw2, id,callback) {
  if (mobile.length != 11) {
    callback.fail(2, "手机号码格式不正确")
    return;
  }
  if (psw1.length == 0) {
    callback.fail(2, "请输入密码")
    return;
  }
  if (psw1 != psw2) {
    callback.fail(2, "两次密码不一致")
    return;
  }
  if (psw1.length < 6 || psw2.length < 6) {
    callback.fail(2, "密码至少6位")
    return;
  }
  if (psw2.length == 0) {
    callback.fail(2, "请确认密码")
    return;
  }

  net.request({
    url: '/FindPsw',
    data: {
      mobile: mobile,
      code: valid,
      psw: psw1,
      shop_id:id
    },
    success(res) {
      var dt = res.data;
      if (callback.success && dt.flag) callback.success(3, dt.msg);
      else callback.fail(2, dt.msg);
    }, fail(res) {
      if (callback.fail) callback.fail(1, "网络异常");
    },complete(){
      if (callback.complete) callback.complete();
    }
  })
}

function resetpsw(mobile,psw1,psw2,psw3,id,callback) {
  if (mobile.length != 11) {
    callback.fail(2,"手机号码格式不正确")
    return;
  }
  if (!id) {
    callback.fail(2, "请选择店铺");
    return;
  }
  if (psw1.length==0) {
    callback.fail(2,"请输入密码")
    return;
  }
  if (psw1.length < 6 || psw2.length<6){
    callback.fail(2,"密码至少6位" )
    return;
  }
  if (psw2.length==0) {
    callback.fail(2,"请确认密码")
    return;
  }
  if (psw3!==psw2){
    callback.fail(2, "输入的密码不一致")
    return;
  }

  net.request({
    url: '/ModifyPsw',
    data: {
      mobile: mobile,
      old_psw: psw1,
      new_psw: psw2,
      shop_id: gd.shop_id,
      member_id: gd.id
    },
    success(res) {
      var dt = res.data;
      if (callback.success && dt.flag) callback.success(3,dt.msg);
      else callback.fail(2,dt.msg);
    }, fail(res) {
      if (callback.fail) callback.fail(1, "网络异常");
    }
  })
}

function login(mobile,psw,id, callback) {
  if (mobile.length != 11) {
    callback.fail(2,"手机号码格式不正确")
    return;
  }
  if (psw.length == 0) {
    callback.fail(2,"请输入密码")
    return;
  }
  if (!id){
    callback.fail(2, "请选择店铺")
    return;
  }
  net.request({
    url: '/Login',
    data: {
      mobile: mobile,
      psw: psw,
      shop_id:id
    },
    success(res) {
      var dt = res.data;
      console.log("dt",dt);
      if (callback.success&&dt.flag) {
        gd.face1 = dt.data.face1;
        gd.face1_status = dt.data.face1_status;
        gd.id = dt.data.member_id;
        gd.shop_id = id;
        gd.name = dt.data.member_name;
        gd.shop_name = dt.data.shop_name;
        gd.mobile = mobile;
        wx.setStorageSync("face1", dt.data.face1)
        wx.setStorageSync("face1_status", dt.data.face1_status)
        wx.setStorageSync("id", dt.data.member_id)
        wx.setStorageSync("shop_id", id)
        wx.setStorageSync("mobile", mobile)
        wx.setStorageSync("name", dt.data.member_name)
        wx.setStorageSync("shop_name", dt.data.shop_name)
        wx.setStorageSync("nuber", dt.data.member_auth)
        console.log("nube", wx.getStorageSync("nuber"));
        console.log("shop", wx.getStorageSync("shop_id"));
        callback.success(3,"登录成功");
      }
      else callback.fail(1, dt.msg);
    }, fail(res) { 
      if (callback.fail) callback.fail(1, "网络异常");
    },complete(){
      if (callback.complete) callback.complete();
    }
  })
}

module.exports={
  login: login,
  reset: resetpsw,
  get_code: get_valid_code,
  forgetpsw: forgetpsw
}